import { UrlService } from './../../../providers/url-service';
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { App } from 'ionic-angular/components/app/app';

import { LoadingController } from 'ionic-angular/components/loading/loading-controller';
import { Api } from '../../../providers/api-service';
import { AgreementPage } from '../../user/user-agreement/agreement';
import { NoticeService } from '../../../providers/notice-service';


@Component({
  selector: 'page-register',
  templateUrl: 'register.html'
})
export class RegisterPage {
  vcode;
  password;
  mobile;
  loading;
  verifyCodeWait = false;
  vcodeName = "获取验证码";
  disable = 'disable';
  constructor(public navCtrl: NavController,
    public loadingCtrl: LoadingController,
    public api: Api,
    public url: UrlService,
    public noticeService: NoticeService,
    public app: App) { }

  /**
 * 输入手机号
 * @param mobileValue 手机
 */
  onMobileChange(mobileValue) {
    this.mobile = mobileValue;
  }

	/**
 * 输入验证码
 */
  onVcodeChange(vcodeValue) {
    this.vcode = vcodeValue;
  }

	/**
   * 查看
   * 来战用户注册协议
   */
  goReAament() {
    this.navCtrl.push(AgreementPage);
  }

  getvcode() {
    //判断手机是否可以注册
    if (this.mobile == null || this.mobile == "") {
      this.noticeService.showNoticeByToast(1, "手机号不能为空");
      return false;
    }  else if (!(/^1[34578]\d{9}$/.test(this.mobile))) {
      this.api.showNoticeByToast(1, "请输入正确的格式手机号码！");
      return false;
    } else {
      this.url.CheckPhoneNumber(this.mobile)
        .then(data => {
          //console.log(data);
          if (data.Code == 2) {
            this.Getcode();//获取手机验证码
          } else {
            this.noticeService.showNoticeByToast(1, data.Msg);
          }
        })
    }
  }
  vcodetime = 60;
  countDownTimer;
  countDown() {
    this.countDownTimer = setInterval(() => {
      this.vcodetime--;
      if (this.vcodetime === 0) {
        this.verifyCodeWait = false;
        this.vcodetime = 60;
        clearInterval(this.countDownTimer);
      }
    }, 1000)
  }

  ionViewWillUnload() {
    clearInterval(this.countDownTimer);
  }
  /**
   * 获取手机验证码
   */
  Getcode() {
    this.verifyCodeWait = true;
    this.countDown();
    this.url.getVcode(this.mobile, 1)
      .then(data => {
        if (data.Code == 0) {
          // this.noticeService.showNoticeByToast(1, data.Msg);
        } else {
          this.noticeService.showNoticeByToast(1, data.Msg);
        }
      })
      .catch(error => {

      })
  }

  chakanPwd = true;
  /**
   * 查看密码
   */
  chaPwd() {
    this.chakanPwd = !this.chakanPwd;
  }
  IsChecked = false;
	/**
   * 提交信息
   */
  submit() {

    if (!(this.IsChecked)) {
      return false;
    } else if (this.mobile == null || this.mobile == "") {
      this.noticeService.showNoticeByToast(1, "手机号不能为空");
      return false;
    } else   if (!(/^1[34578]\d{9}$/.test(this.mobile))) {
      this.api.showNoticeByToast(1, "请输入正确的格式手机号码！");
      return false;
    }  else if (this.vcode == undefined || this.vcode.length == 0) {
      this.api.showNoticeByToast(2, "请输入验证码");
      return false;
    } else if (this.password == undefined || this.password.length == 0) {
      this.api.showNoticeByToast(2, "请设置密码");
      return false;
    }

    this.loading = this.loadingCtrl.create({
      content: '请稍后...'
    });
    this.loading.present();
    this.url.setRegister(this.mobile, this.vcode, this.password)
      .then(msg => {
        if (msg.Code == 0) {
          this.navCtrl.pop();
        } else {
          this.noticeService.showNoticeByToast(1, msg.Msg);
        }
        this.loading.dismiss();
      }).catch(err => {
        this.loading.dismiss();
        this.api.showTip(err);
      });

  }
}
